package cn.wx.system.controller;


import cn.wx.basic.util.JsonResult;
import cn.wx.basic.util.PageList;
import cn.wx.system.domain.Dictionarytype;
import cn.wx.system.query.DictionarytypeQuery;
import cn.wx.system.service.IDictionarytypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dictionarytype")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class DictionarytypeController {

    @Autowired
    private IDictionarytypeService iDictionarytypeService;

    //使用get方式请求  查询所有
    @GetMapping
    @ApiOperation(value = "查询所有" )
    public List<Dictionarytype> findAll(){
        return iDictionarytypeService.loadAll();
    }

    @GetMapping("/{id}")   //获取get请求里面的参数   查询一个
    @ApiOperation(value = "通过ID查询" )
    public  Dictionarytype findById(@PathVariable("id") Long id ){
        return iDictionarytypeService.loadById(id);
    }

    //删除
    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过ID删除" )
    public JsonResult delete(@PathVariable("id") Long id){
        try {
            iDictionarytypeService.remove(id);
           return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    //添加和更新     //使用put方式请求
    @PutMapping
    @ApiOperation(value = "增加修改" )
    public JsonResult save(@RequestBody  Dictionarytype  Dictionarytype){
        try {
            if( Dictionarytype.getId()==null){
                iDictionarytypeService.save( Dictionarytype);
            }else{
                iDictionarytypeService.update( Dictionarytype);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    //分页
    @PostMapping      //使用post方式请求
    @ApiOperation(value = "分页查询" )
    public PageList< Dictionarytype> queryPage(@RequestBody DictionarytypeQuery query){

        try {
            return  iDictionarytypeService.pageQuery(query);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<>();
        }
    }

    //批量删除
    @PatchMapping
    public JsonResult  patchDel(@RequestBody List<Long> ids){
        try {
            iDictionarytypeService.patchDel(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }

    }


}
